#include "StdAfx.h"
#include "DepthArea.h"

DepthArea::DepthArea(void)
{
}

DepthArea::~DepthArea(void)
{
}

Node* DepthArea::LoadDepthArea(const string& s57file, const string& shpDir, const vector<LevelColor>& levelColors)
{
	Group* group = new Group;

	unsigned levelIndex = 1;
	for (; levelIndex < levelColors.size(); ++levelIndex)
	{
		ostringstream outFileName;
		outFileName << "DEPARE_" << levelColors[levelIndex-1].level << "_" << levelColors[levelIndex].level;
		string shpFile(shpDir);
		shpFile += "/";
		shpFile += outFileName.str();
		shpFile += ".shp";
		
		Node* node = loadShpFileAndSetColor(shpFile,
											levelColors[levelIndex].r / 255.0f,
											levelColors[levelIndex].g / 255.0f,
											levelColors[levelIndex].b / 255.0f,
											levelColors[levelIndex].a / 255.0f);
		if (node)
		{
			group->addChild(node);
			continue;
		}

		char** cmd = new char*[11];
		cmd[0] = "ogr2ogr";
		cmd[1] = "-skipfailure";
		cmd[2] = "-where";
		cmd[3] = new char[60];
		cmd[4] = "-nln";
		cmd[5] = new char[100];
		cmd[6] = new char[100];
		cmd[7] = new char[100];
		cmd[8] = "-nlt";
		cmd[9] = "polygon";
		cmd[10] = "DEPARE";
		sprintf(cmd[3], "DRVAL2>%d and DRVAL2<=%d", levelColors[levelIndex-1].level, levelColors[levelIndex].level);
		sprintf(cmd[5], outFileName.str().c_str());
		strcpy(cmd[6], shpDir.c_str());
		strcpy(cmd[7], s57file.c_str());

		ogr2ogr(11, cmd);

		delete [] cmd[3];
		delete [] cmd[5];
		delete [] cmd[6];
		delete [] cmd[7];
		delete [] cmd;

		node = loadShpFileAndSetColor(shpFile,
									  levelColors[levelIndex].r / 255.0f,
									  levelColors[levelIndex].g / 255.0f,
									  levelColors[levelIndex].b / 255.0f,
									  levelColors[levelIndex].a / 255.0f);
		if (node)
		{
			group->addChild(node);
		}
	}

	return group;
}
